﻿Imports System
Imports System.Drawing
Imports System.Drawing.Drawing2D
Imports System.Windows.Forms

Namespace System.Workflow.ComponentModel.Design
    Public Class ConfigErrorGlyph
        Inherits DesignerGlyph
        ' Methods
        Public Overrides Function GetBounds(ByVal designer As ActivityDesigner, ByVal activated As Boolean) As Rectangle
            If (designer Is Nothing) Then
                Throw New ArgumentNullException("designer")
            End If
            Dim glyphSize As Size = WorkflowTheme.CurrentTheme.AmbientTheme.GlyphSize
            Dim margin As Size = WorkflowTheme.CurrentTheme.AmbientTheme.Margin
            Dim location As New Point(((designer.Bounds.Right - glyphSize.Width) - (margin.Width / 2)), ((designer.Bounds.Top - glyphSize.Height) + margin.Height))
            Dim rectangle As New Rectangle(location, glyphSize)
            If activated Then
                rectangle.Width = (rectangle.Width * 2)
                Dim ambientTheme As AmbientTheme = WorkflowTheme.CurrentTheme.AmbientTheme
                rectangle.Inflate((ambientTheme.Margin.Width / 2), (ambientTheme.Margin.Height / 2))
            End If
            Return rectangle
        End Function

        Protected Overrides Sub OnActivate(ByVal designer As ActivityDesigner)
            If ((Not designer Is Nothing) AndAlso (designer.DesignerActions.Count > 0)) Then
                Dim bounds As Rectangle = Me.GetBounds(designer, False)
                Dim location As Point = designer.ParentView.LogicalPointToScreen(New Point(bounds.Left, bounds.Bottom))
                DesignerHelpers.ShowDesignerVerbs(designer, location, DesignerHelpers.GetDesignerActionVerbs(designer, designer.DesignerActions))
            End If
        End Sub

        Protected Overrides Sub OnPaint(ByVal graphics As Graphics, ByVal activated As Boolean, ByVal ambientTheme As AmbientTheme, ByVal designer As ActivityDesigner)
            Dim bounds As Rectangle = Me.GetBounds(designer, False)
            Dim rect As Rectangle = Me.GetBounds(designer, activated)
            Dim region As Region = Nothing
            Dim clip As Region = graphics.Clip
            Try 
                If (Not clip Is Nothing) Then
                    [region] = clip.Clone
                    If activated Then
                        [region].Union(rect)
                    End If
                    graphics.Clip = [region]
                End If
                If activated Then
                    graphics.FillRectangle(SystemBrushes.ButtonFace, rect)
                    graphics.DrawRectangle(SystemPens.ControlDarkDark, rect.Left, rect.Top, (rect.Width - 1), (rect.Height - 1))
                    rect.X = (rect.X + (bounds.Width + ambientTheme.Margin.Width))
                    rect.Width = (rect.Width - (bounds.Width + (2 * ambientTheme.Margin.Width)))
                    [Using] path As GraphicsPath = ActivityDesignerPaint.GetScrollIndicatorPath(rect, ScrollButton.Down)
                        graphics.FillPath(SystemBrushes.ControlText, path)
                        graphics.DrawPath(SystemPens.ControlText, path)
                    End [Using]
                End If
                ActivityDesignerPaint.DrawImage(graphics, AmbientTheme.ConfigErrorImage, bounds, DesignerContentAlignment.Fill)
            Finally
                If (Not [region] Is Nothing) Then
                    graphics.Clip = clip
                    [region].Dispose
                End If
            End Try
        End Sub


        ' Properties
        Public Overrides ReadOnly Property CanBeActivated As Boolean
            Get
                Return True
            End Get
        End Property

        Friend Shared ReadOnly Property [Default] As ConfigErrorGlyph
            Get
                If (ConfigErrorGlyph.defaultConfigErrorGlyph Is Nothing) Then
                    ConfigErrorGlyph.defaultConfigErrorGlyph = New ConfigErrorGlyph
                End If
                Return ConfigErrorGlyph.defaultConfigErrorGlyph
            End Get
        End Property

        Public Overrides ReadOnly Property Priority As Integer
            Get
                Return 2
            End Get
        End Property


        ' Fields
        Private Shared defaultConfigErrorGlyph As ConfigErrorGlyph
    End Class
End Namespace

